package com.cooksys.film_rental.bo.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cooksys.film_rental.bo.PaymentBo;
import com.cooksys.film_rental.bo.RentalBo;
import com.cooksys.film_rental.bo.StaffBo;
import com.cooksys.film_rental.bo.StoreBo;
import com.cooksys.film_rental.dao.RentalDao;
import com.cooksys.film_rental.model.Customer;
import com.cooksys.film_rental.model.Inventory;
import com.cooksys.film_rental.model.Payment;
import com.cooksys.film_rental.model.Rental;
import com.cooksys.film_rental.model.Staff;
import com.cooksys.film_rental.model.Store;

@Service("RentalBo")
public class RentalBoImpl implements RentalBo {

	@Autowired(required = true)
	RentalDao rentalDao;

	@Autowired
	StoreBo storeBo;

	@Autowired
	StaffBo staffBo;

	@Autowired
	PaymentBo paymentBo;

	@Override
	public Rental getRentalByRentalId(Integer rentalId) {
		return this.rentalDao.getRentalByRentalId(rentalId);
	}

	@Override
	public Rental getRentalByRentalId(String rentalId) {
		try {

			Integer rentalIdAsInteger = Integer.valueOf(rentalId);
			return this.getRentalByRentalId(rentalIdAsInteger);

		} catch (NumberFormatException e) {

			System.err
					.println("Could not parse Integer value for rentalId from string: "
							+ rentalId);
			e.printStackTrace();
			System.err.println("Returning null value...");
			return null;

		}
	}

	@Override
	public List<Rental> searchRentalsByStaff(Staff staff) {
		return this.rentalDao.searchRentalsByStaff(staff);
	}

	@Override
	public List<Rental> searchRentalsByCustomer(Customer customer) {
		return this.rentalDao.searchRentalsByCustomer(customer);
	}

	@Override
	public List<Rental> searchRentalsByInventory(Inventory inventory) {
		return this.rentalDao.searchRentalsByInventory(inventory);
	}

	@Override
	public Rental getRentalByPayment(Payment payment) {
		return this.rentalDao.getRentalByPayment(payment);
	}

	@Override
	public void rentInventory(Customer customer, Inventory inventory) {
		Store customerStore = storeBo.getStoreByCustomer(customer);
		Staff rentalStaff = staffBo.getStaffByStore(customerStore);

		Rental newRental = new Rental();

		newRental.setRentalDate(new Date());
		newRental.setInventory(inventory);
		newRental.setCustomer(customer);
		newRental.setStaff(rentalStaff);

		this.save(newRental);

	}

	@Override
	public Rental save(Rental rental) {
		return this.rentalDao.save(rental);
	}

	@Override
	public void delete(Rental rental) {
		this.rentalDao.delete(rental);

	}

	public void update(Rental rental) {
		this.rentalDao.update(rental);
	}

	@Override
	public void payRental(Rental rental, Customer customer) {

		Payment payment = new Payment();

		payment.setAmount(rental.getInventory().getFilm().getRentalRate());
		payment.setCustomer(customer);
		payment.setPaymentDate(new Date());
		payment.setRental(rental);
		payment.setStaff(staffBo.getStaffByStore(storeBo
				.getStoreByCustomer(customer)));

		paymentBo.save(payment);
	}

	@Override
	public void returnRental(Rental rental) {
		rental.setReturnDate(new Date());
		this.update(rental);
	}

}
